﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;

namespace CollectionViewSourceSpikes {
    public partial class MainPage : UserControl {

        private CollectionViewSource viewSource_ = null;
        private ObservableCollection<Item> items = null;
        public MainPage() {
            InitializeComponent();
            items = CreateItems();
            viewSource_ = new CollectionViewSource();
            viewSource_.Source = items;
            WriteText();
            groupingView.ItemsSource = viewSource_.View;

            SampleListBox.ItemsSource = items;
            SampleTreeView.ItemsSource = items;

            this.GotFocus += new RoutedEventHandler(MainPage_GotFocus);

        }


        void MainPage_GotFocus(object sender, RoutedEventArgs e) {
            ItemsTextBox.Text = e.OriginalSource.GetType().Name;
        }

        /// <summary>
        /// カテゴリを大きい順番でソート
        /// </summary>
        private void Button_Click(object sender, RoutedEventArgs e) {
            using (viewSource_.DeferRefresh()) {
                viewSource_.SortDescriptions.Clear();
                viewSource_.SortDescriptions.Add(new SortDescription("Category", ListSortDirection.Descending));
            }
            WriteText();
        }
        /// <summary>
        /// カテゴリを小さい順番でソート
        /// </summary>
        private void Button_Click_1(object sender, RoutedEventArgs e) {
            using (viewSource_.DeferRefresh()) {
                viewSource_.SortDescriptions.Clear();
                viewSource_.SortDescriptions.Add(new SortDescription("Category", ListSortDirection.Ascending));
            }
            WriteText();
        }

        /// <summary>
        /// ソートを解除
        /// </summary>
        private void Button_Click_2(object sender, RoutedEventArgs e) {
            using (viewSource_.DeferRefresh()) {
                viewSource_.SortDescriptions.Clear();
            }
            WriteText();
        }

        /// <summary>
        /// カテゴリでグルーピング
        /// </summary>
        private void Button_Click_3(object sender, RoutedEventArgs e) {
            using (viewSource_.DeferRefresh()) {
                viewSource_.GroupDescriptions.Clear();
                viewSource_.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
            }
            WriteText();        
        }

        /// <summary>
        /// カテゴリと日付でグルーピング
        /// </summary>
        private void Button_Click_3_1(object sender, RoutedEventArgs e) {
            using (viewSource_.DeferRefresh()) {
                viewSource_.GroupDescriptions.Clear();
                viewSource_.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
                viewSource_.GroupDescriptions.Add(new PropertyGroupDescription("Day"));
            }
            WriteText();
        }

        /// <summary>
        /// カテゴリのグルーピングを解除
        /// </summary>
        private void Button_Click_4(object sender, RoutedEventArgs e) {
            using (viewSource_.DeferRefresh()) {
                viewSource_.GroupDescriptions.Clear();
            }
            WriteText();
        }

        /// <summary>
        /// カテゴリDを追加
        /// </summary>
        private void Button_Click_5(object sender, RoutedEventArgs e) {
            using (viewSource_.DeferRefresh()) {
                items.Add(CreateNewItem());
            }
            WriteText();
        }

        /// <summary>
        /// カテゴリDを複数追加
        /// </summary>
        private void Button_Click_6(object sender, RoutedEventArgs e) {
            using (viewSource_.DeferRefresh()) {
                items.Add(CreateNewItem());
                items.Add(CreateNewItem());
            }
            WriteText();
        }

        private void Button_Click_7(object sender, RoutedEventArgs e) {
            using (viewSource_.DeferRefresh()) {
                groupingView.SelectedItem = null;
            }
            WriteText();
        }

        private Item CreateNewItem() {
            Random r = new Random();
            int year = r.Next(99);
            return new Item() {
                Category = "Category D",
                Day = new DateTime(2000 + year, 10, 10),
                ItemName = "20" + year.ToString("00") + "/01/01 D"
            };
        }

        private void WriteText() {
            List<string> itemTexts = new List<string>();
            
            if(viewSource_.View.Groups != null && viewSource_.View.Groups.Count > 0){
                foreach (CollectionViewGroup group in viewSource_.View.Groups) {
                    WriteGroup(itemTexts, group);
                }
            }else{
                foreach (Item item in viewSource_.View) {
                    itemTexts.Add(item.ToString());
                }
            }
            
            //ItemsTextBox.Text = string.Join("\r\n", itemTexts.ToArray());
        }

        private void WriteGroup(List<string> itemTexts,CollectionViewGroup group){
            itemTexts.Add(string.Format("[{0}]", group.Name));
            if(!group.IsBottomLevel){
                foreach(CollectionViewGroup childGroup in group.Items){
                    WriteGroup(itemTexts, childGroup);
                }
            }else{
                foreach (object child in group.Items) {
                    itemTexts.Add(child.ToString());
                }
            }
        }

        private ObservableCollection<Item> CreateItems() {
            ObservableCollection<Item> items = new ObservableCollection<Item>();
            items.Add(new Item(){
                Day = new DateTime(2010,1,1),
                Category = "Category A",
                ItemName = "2010/01/01 A 1"
            });
            items.Add(new Item() {
                Day = new DateTime(2010, 1, 2),
                Category = "Category A",
                ItemName = "2010/01/02 A 2"
            });
            items.Add(new Item() {
                Day = new DateTime(2010, 1, 1),
                Category = "Category B",
                ItemName = "2010/01/01 B"
            });
            items.Add(new Item() {
                Day = new DateTime(2010, 1, 1),
                Category = "Category C",
                ItemName = "2010/01/01 C"
            });
            items.Add(new Item() {
                Day = new DateTime(2010, 1, 2),
                Category = "Category A",
                ItemName = "2010/01/02 A"
            });
            items.Add(new Item() {
                Day = new DateTime(2010, 1, 2),
                Category = "Category B",
                ItemName = "2010/01/02 B"
            });
            items.Add(new Item() {
                Day = new DateTime(2010, 1, 3),
                Category = "Category A",
                ItemName = "2010/01/03 A"
            });
            items.Add(new Item() {
                Day = new DateTime(2010, 1, 3),
                Category = "Category C",
                ItemName = "2010/01/03 C"
            });

            return items;
        }

        
    }

    public class Item {

        public DateTime Day { get; set; }

        public string Category { get; set; }

        public String ItemName { get; set; }

        public override string ToString() {
            return ItemName;
        }
    }
}
